﻿// 基础UI窗体     
// 功能：所有用户UI窗体的父类
// 1：定于四个“UI窗体”的状态
// Display:    显示状态
// Hiding:     隐藏状态(即：不能看见，不能操作)
// Redisplay:  重新显示状态
// Freeze:     冻结状态(即：在其他窗体下面，看见但不能操作)

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System;
using System.Collections.Generic;

namespace UIKit
{
    public class BaseUIForms : MonoBehaviour
    {
        /*  字段  */
        //当前(基类)窗口的类型
        private UIType _CurrentUIType = new UIType();

        /*  属性  */
        /// <summary>
        /// 属性_当前UI窗体类型
        /// </summary>
        public UIType CurrentUIType => _CurrentUIType;

        /// <summary>
        /// UI管理器
        /// </summary>
        UIManager Manager => UIManager.GetInstance();

        #region 窗体生命周期

        /// <summary>
        /// 初始化窗体
        /// </summary>
        public virtual void OnInit()
        {
            /*
            //是否需要清空“反向切换”
            CurrentUIType.IsClearReverseChange = false;
            //UI窗体类型
            CurrentUIType.UIForms_Type = UIFormsType.Normal;
            //UI窗体显示类型
            CurrentUIType.UIForms_ShowMode = UIFormsShowMode.Normal;
            //UI窗体透明度类型
            CurrentUIType.UIForms_LucencyType = UIFormsLucencyType.Lucency;
            */            
        }

        /// <summary>
        /// 打开窗体
        /// </summary>
        public virtual void OnOpen() {}

        /// <summary>
        /// 重新打开窗体
        /// </summary>
        public virtual void OnReOpen() {}

        /// <summary>
        /// 关闭窗体
        /// </summary>
        public virtual void OnClose() {}

        /// <summary>
        /// 冻结窗体
        /// </summary>
        public virtual void OnFreeze() {}

        //初始化
        internal void Init()
        {
            OnInit();
        }

        //页面显示
        internal void Open()
        {
            this.gameObject.SetActive(true);
            if (_CurrentUIType.UIForms_Type == UIFormsType.PopUp 
            || _CurrentUIType.UIForms_Type == UIFormsType.TopUp) 
            {
                //添加UI遮罩处理
                UIMaskMgr.GetInstance().SetMaskWindow(this.gameObject,_CurrentUIType.UIForms_LucencyType);                
            }

            // 自动注册MVC View
            //RegisterViewCommand();
            OnOpen();
        }

        //页面隐藏(不在“栈”集合中)
        internal void Close()
        {
            this.gameObject.SetActive(false);
            if (_CurrentUIType.UIForms_Type == UIFormsType.PopUp
            || _CurrentUIType.UIForms_Type == UIFormsType.TopUp)
            {
                //添加UI遮罩处理
                UIMaskMgr.GetInstance().CancleMaskWindow();
            }

            // 自动销毁MVC View
            //DestroyViewCommand();          
            OnClose();
        }

        //页面重新显示
        internal void ReOpen()
        {
            this.gameObject.SetActive(true);
            if (_CurrentUIType.UIForms_Type == UIFormsType.PopUp
            || _CurrentUIType.UIForms_Type == UIFormsType.TopUp)
            {
                //添加UI遮罩处理
                UIMaskMgr.GetInstance().SetMaskWindow(this.gameObject, _CurrentUIType.UIForms_LucencyType);
            }

            OnReOpen();
        }

        //页面冻结(还在“栈”集合中)
        internal void Freeze()
        {
            this.gameObject.SetActive(true);
            OnFreeze();
        } 
        #endregion

        #region 给子类封装的方法

        /// <summary>
        /// 是否有效名称
        /// </summary>
        private bool InvalidName(string name)
        {
            return string.IsNullOrEmpty(name);
        }

        /// <summary>
        /// 打开UI界面 - 名称
        /// </summary>
        protected void ShowUIForms(string name)
        {
            if (!InvalidName(name))
                Manager.ShowUIForms(name);
        }

        /// <summary>
        /// 打开UI界面 - 模板
        /// </summary>
        protected void ShowUIForms<T>() where T : BaseUIForms
        {
            string name = typeof(T).Name;
            if (!InvalidName(name))
                Manager.ShowUIForms(name);
        }

        /// <summary>
        /// 关闭UI界面 - 名称
        /// </summary>
        protected void CloseUIForms(string name)
        {
            if (!InvalidName(name))
                Manager.CloseUIForms(name);
        }

        /// <summary>
        /// 关闭UI界面 - 模板
        /// </summary>
        protected void CloseUIForms<T>() where T : BaseUIForms
        {
            string name = typeof(T).Name;
            if (!InvalidName(name))
                Manager.CloseUIForms(name);
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="strMsgType">消息大类</param>
        /// <param name="strSmallClassType">消息小类</param>
        /// <param name="strMsgContent">消息内容</param>
        protected void SendMessage(string strMsgType, string strSmallClassType, object objMsgContent)
        {
            KeyValuesUpdate kv = new KeyValuesUpdate(strSmallClassType, objMsgContent);
            MessageCenter.SendMessage(strMsgType, kv);
        }

        /// <summary>
        /// 显示语言信息
        /// </summary>
        /// <param name="info"></param>
        protected string ShowText(string info)
        {
            return LauguageMgr.GetInstance().ShowText(info);
        }

        #endregion

        #region UI 查找相关
        /// <summary>
        /// 获取UI控件 - 自动绑定功能
        /// </summary>
        protected virtual void InitializeComponent() {}

        /// <summary>
        /// 查找节点 - 名称
        /// </summary>
        protected Transform Find(string n)
        {
            return Prefab.Find(transform, n);
        }

        /// <summary>
        /// 获取节点 - 索引
        /// </summary>
        protected Transform GetChild(int index)
        {
            return Prefab.GetChild(transform, index);
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        protected void AddClickEventListener(string n, EventTriggerListener.VoidDelegate callback)
        {
            Prefab.AddClickEventListener(transform, n, callback);
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        protected void AddClickEventListener(Transform transform, EventTriggerListener.VoidDelegate callback)
        {
            Prefab.AddClickEventListenerByTransform(transform, callback);
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        protected void AddClickEventListener(GameObject gameObject, EventTriggerListener.VoidDelegate callback)
        {
            Prefab.AddClickEventListenerByGameObject(gameObject, callback);
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        protected void AddClickEventListener(Toggle component, EventTriggerListener.BoolDelegate callback)
        {
            if (component != null)
            {
                component.onValueChanged.AddListener((enabled) => {
                    if (callback != null)
                    {
                        callback(component.gameObject, enabled);
                    }
                });
            }
        }

        #endregion

    }//Class_end
}
